<?php

namespace App\Services\Sms;

use Qcloud\Sms\SmsSingleSender;

class CardSmsService
{
    public static function recharge($mobile, $name, $amount, $balance)
    {
        try
        {
            $sender = new SmsSingleSender(env('SMS_AP_PID'), env('SMS_APP_KEY'));
            $result = $sender->sendWithParam(
                "86",
                $mobile, env('SMS_TEMPLATE_CARD_RECHARGE'),
                ["$name", "$amount", "$balance"],
                "",
                "",
                ""
            );
            $rsp    = json_decode($result, true);
        }
        catch (\Exception $e)
        {
            LogService::error('Sms Error: '.$e->getMessage());

            return false;
        }

        // 网关消息
        if (isset($rsp['ActionStatus']) && 'FAIL' == $rsp['ActionStatus'])
        {
            LogService::error('Sms Error: '.$rsp['ErrorCode'].' '.$rsp['ErrorInfo']);

            return false;
        }

        // 网关消息
        if (isset($rsp['result']) && $rsp['result'])
        {
            LogService::error('Sms Error: '.$rsp['errmsg']);

            return false;
        }

        return true;
    }

    public static function expense($mobile, $name, $amount, $balance)
    {
        try
        {
            $sender = new SmsSingleSender(env('SMS_AP_PID'), env('SMS_APP_KEY'));
            $result = $sender->sendWithParam(
                "86",
                $mobile, env('SMS_TEMPLATE_CARD_EXPENSE'),
                ["$name", "$amount", "$balance"],
                "",
                "",
                ""
            );
            $rsp    = json_decode($result, true);
        }
        catch (\Exception $e)
        {
            LogService::error('Sms Error: '.$e->getMessage());

            return false;
        }

        // 网关消息
        if (isset($rsp['ActionStatus']) && 'FAIL' == $rsp['ActionStatus'])
        {
            LogService::error('Sms Error: '.$rsp['ErrorCode'].' '.$rsp['ErrorInfo']);

            return false;
        }

        // 网关消息
        if (isset($rsp['result']) && $rsp['result'])
        {
            LogService::error('Sms Error: '.$rsp['errmsg']);

            return false;
        }

        return true;
    }
}
